package com.robusta.app.model.spreadsheet.loader;

import com.robusta.spreadsheet.model.*;
import com.robusta.spreadsheet.model.exception.WorkbookException;

import java.util.Iterator;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

public class WorkbookFixture implements Workbook {
    @Override
    public Worksheet getSheetFor(WorksheetTemplate template) throws WorkbookException {
        return new WorksheetFixture();
    }

    private class WorksheetFixture implements Worksheet {
        private RowsFixture rowsFixture = new RowsFixture(25);

        @Override
        public SheetHeaders getHeaders() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Row getRowAt(int rowIndex) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Iterator<Row> iterator() {
            return rowsFixture.iterator();
        }

        @Override
        public Workbook owner() {
            throw new UnsupportedOperationException();
        }
    }

    private class RowsFixture implements Iterable<Row> {
        private List<Row> rows = newArrayList();
        public RowsFixture(int countRows) {
            for(int i = 0; i < countRows; i++) {
                rows.add(new RowFixture(i));
            }
        }

        @Override
        public Iterator<Row> iterator() {
            return rows.iterator();
        }
    }

    public static class RowFixture implements Row {
        private int index;

        public RowFixture(int index) {
            this.index = index;
        }

        @Override
        public Cell getCellAt(int position) throws WorkbookException {
            throw new UnsupportedOperationException();
        }

        @Override
        public Cell getCellFor(HeaderCell headerCell) throws WorkbookException {
            throw new UnsupportedOperationException();
        }

        @Override
        public Cell getCellByHeaderText(final String headerText) throws WorkbookException {
            return new Cell() {
                @Override
                public String getValue() {
                    return "Row value of header: " + headerText + " at row: " + index;
                }

                @Override
                public HeaderCell getHeader() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public Row owner() {
                    throw new UnsupportedOperationException();
                }
            };
        }

        @Override
        public int rowIndex() {
            return index;
        }

        @Override
        public Iterator<Cell> iterator() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Worksheet owner() {
            throw new UnsupportedOperationException();
        }
    }
}
